
import { baseUrl1 } from '@/config/index'
import { showToast } from '@/utils/index'
import { getAccessToken } from '@/utils/auth'
export default function request<T>(options: UniApp.RequestOptions): Promise<T> {
    return new Promise((resolve, reject) => {
        uni.request({
            url: baseUrl1 + options.url,
            method: options.method || 'GET',
            data: Object.assign({}, options.data || {}),
            header: {
                'Content-Type': 'application/json',
                'Access-Token': getAccessToken()
            },
            success: (res) => {
                console.log('res', res)
                if (res.statusCode === 200) {
                    if ((res.data as T & { code: string }).code == '0') {
                        resolve(res.data as T) // Update this line
                    } else {
                        showToast((res.data as T & { message?: string }).message || '请求错误') // Update this line
                        reject(res.data)
                    }
                } else {
                    showToast((res.data as T & { message?: string }).message || '请求错误') // Update this line
                    reject(res.data)
                }
            },
            fail: (err) => {
                showToast('请求失败')
                reject(err)
            }
        })
    })
}